package routes

import (
	"fmt"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"k8s-api/controllers"
	"k8s-api/controllers/pod"
	_ "k8s-api/docs"
	"net/http"
)

func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger())

	r.GET("/wss", pod.Ssh)

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "hello world",
		})
	})

	r.GET("/allInfo", controllers.GetAllInfoHandler)

	r.POST("/postJson", func(c *gin.Context) {
		json := make(map[string]interface{})
		c.BindJSON(&json)
		value := json["domain"]
		domains := value.([]interface{})
		for _, v := range domains {
			switch v.(type) {
			case string:
				fmt.Println(v)
			}
		}
		c.JSON(http.StatusOK, gin.H{
			"data": "ok",
		})
	})

	apiV1UseMiddle := r.Group("/api/v1/kubernetes")
	{
		apiV1UseMiddle.GET("/namespace/:namespace/deployment/:deployment", controllers.GetDeploymentHandler)
		apiV1UseMiddle.POST("/namespace/:namespace/deployment/:deployment", controllers.CreateDeploymentHandler)
		apiV1UseMiddle.DELETE("/namespace/:namespace/deployment/:deployment", controllers.DeleteDeploymentHandler)
		apiV1UseMiddle.PUT("/namespace/:namespace/deployment/:deployment", controllers.UpdateDeploymentHandler)

		apiV1UseMiddle.GET("/namespace/:namespace/service/:service", controllers.GetServiceHandler)
		apiV1UseMiddle.POST("/namespace/:namespace/service/:service", controllers.CreateServiceHandler)
		apiV1UseMiddle.DELETE("/namespace/:namespace/service/:service", controllers.DeleteServiceHandler)

		apiV1UseMiddle.GET("/namespace/:namespace/ingress/:ingress", controllers.GetIngressHandler)
		apiV1UseMiddle.POST("namespace/:namespace/ingress/:ingress/domain/:domain", controllers.CreateIngressHandler)
		apiV1UseMiddle.DELETE("/namespace/:namespace/ingress/:ingress", controllers.DeleteIngressHandler)
	}

	return r
}
